// pages/upInformation/upInformation.js
const app = getApp();

var latitude, longitude;
Page({
    data: {
        userInfo: {},
        markers: [{
            iconPath: "../img/map.png",
            id: 0,
            title: '选择位置',
            latitude: 0,
            longitude: 0,
        }],
        controls: [{
            id: 1,
            iconPath: "../img/map.png",
            position: {
                left: 150 + 13,
                top: 100 - 30,
                width: 30,
                height: 30
            },
        }],
        userInform: {
            userimg: "",
            username: "",
            usersaid: "",
            userphone: "",
            userwxnumber: "",
            useraddress: [],
            userxiangq: '',
            userbrief: "",

        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        app.globalData.tempUrl="";
        wx.setNavigationBarTitle({
            title: '用户信息',
        });
        this.data.isOpen = true;
        var _this = this;
        app.http.POST("/MiniCardUser/GetCardUserInfo", {}, (err, data) => {
            console.log("GetCardUserInfo", data.data);
            if (data.code == 1) {
                var regionarry = new Array();
                regionarry.push(data.data.Provinece);
                regionarry.push(data.data.City);
                regionarry.push(data.data.District);
                _this.setData({
                    "userInfo": data.data,
                    "userInform": {
                        "username": data.data.NickName,
                        "userimg": data.data.HeadImg,
                        "usersaid": data.data.Title,
                        "userphone": data.data.Phone,
                        "userwxnumber": data.data.WxNumber,
                        "useraddress": regionarry,
                        "userxiangq": data.data.Address,
                        "userbrief": data.data.Summary,
                    },
                })
                console.log(regionarry);
                var Coordinate = data.data.Coordinate;
                if (Coordinate) {
                    var result = Coordinate.split(",");
                    var markers = _this.data.markers;
                    markers[0].latitude = result[1];
                    markers[0].longitude = result[0];
                    _this.setData({
                        "userInfo.longitude": result[0],
                        "userInfo.latitude": result[1],
                    })
                } else {
                    wx.getLocation({
                        type: 'gcj02',
                        success: function (res) {
                            var latitude = res.latitude
                            var longitude = res.longitude
                            console.log(latitude + "," + longitude);
                            _this.setData({
                                "userInfo.longitude": longitude,
                                "userInfo.latitude": latitude
                            })
                        }
                    })
                }
            } else {
                //如果请求失败
                wx.showToast({
                    title: '请求失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },
    //详细地址选择器
    bindRegionChange: function (e) {
        var _this = this;
        var value = e.detail.value;
        var dataArr = _this.data.userInfo;
        dataArr.Provinece = value[0];
        dataArr.City = value[1];
        dataArr.District = value[2];
        var citystring = []
        citystring[0] = value[0];
        citystring[1] = value[1];
        citystring[2] = value[2];
        _this.setData({
            "userInfo": dataArr,
            "userInform.useraddress": citystring
        })
    },
    //获取提交信息
    formSubmit: function (e) {
        var key = e.detail.value;
        // if (key.username == "") {
        //     wx.showToast({
        //         title: '请输入姓名',
        //         icon: 'none',
        //         duration: 2000
        //     })
        //     return;
        // }
        // if (key.usersaid == "") {
        //     wx.showToast({
        //         title: '请输入职称',
        //         icon: 'none',
        //         duration: 2000
        //     })
        //     return;
        // }
        // if (key.userphone == "") {
        //     wx.showToast({
        //         title: '请输入手机号',
        //         icon: 'none',
        //         duration: 2000
        //     })
        //     return;
        // }

        // if (key.userwxnumber == "") {
        //     wx.showToast({
        //         title: '请输入微信号',
        //         icon: 'none',
        //         duration: 2000
        //     })
        //     return;
        // }
        // if (key.useraddress == "") {
        //     wx.showToast({
        //         title: '请选择区域',
        //         icon: 'none',
        //         duration: 2000
        //     })
        //     return;
        // }
        // if (key.userxiangq == "") {
        //     wx.showToast({
        //         title: '请输入地址详情',
        //         icon: 'none',
        //         duration: 2000
        //     })
        //     return;
        // }
        // if (key.userbrief == "") {
        //     wx.showToast({
        //         title: '请输入简介',
        //         icon: 'none',
        //         duration: 2000
        //     })
        //     return;
        // }
        //验证手机号码
        if (key.userphone != "") {
            var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
            if (!myreg.test(key.userphone)) {
                wx.showToast({
                    title: '手机号有误',
                    icon: 'none',
                    duration: 2000
                })
                return;
            }
        }
        var postall = {};
        var _this = this;
        var jwcdu = _this.data.userInfo.longitude
        postall.HeadImg = _this.data.userInform.userimg;
        postall.NickName = key.username;
        postall.Title = key.usersaid;
        postall.Phone = key.userphone;
        postall.WxNumber = key.userwxnumber;
        postall.Provinece = _this.data.userInfo.Provinece;
        postall.City = _this.data.userInfo.City;
        postall.District = _this.data.userInfo.District;
        postall.Address = key.userxiangq;
        postall.Coordinate = _this.data.userInfo.longitude + "," + _this.data.userInfo.latitude;
        postall.Summary = key.userbrief;

        app.http.POST("/MiniCardUser/UpdateCardUser", postall, (err, data) => {
            if (data.code == 1) {
                wx.showToast({
                    title: '成功',
                    icon: 'success',
                    duration: 2000,
                    Boolean: true,
                    success: function (res) {
                      app.globalData.userInfo=data.data;
                        wx.redirectTo({
                            url: '../mycenter/mycenter'
                        })
                    }
                })
            } else {
                wx.showToast({
                    title: '提交失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        })


    },
    //更换头像
    upUserimf: function () {
        wx.navigateTo({
            url: '../imgupload/imgupload?photoid=1'
        })
    },
    //获取定位
    getmaps: function () {
        var _this = this;
        wx.chooseLocation({
            success: function (res) {
                console.log(res);
                _this.setData({
                    "userInform.userxiangq": res.address,
                    "userInfo.latitude": res.latitude,
                    "userInfo.longitude": res.longitude
                });
            }
        })

    },
    getPhone:function(e){
        console.log(e);
        var detail=e.detail;
        var _this=this;
        if (detail.encryptedData && detail.iv&&detail.encryptedData!=""&&detail.iv!=""){
            app.http.POST("/User/GetAuthorizePhone", { "encryptedData": detail.encryptedData, "iv": detail.iv},(err,data)=>{
                if(data.code==1){
                    var phoneData=data.data;
                    _this.setData({
                        "userInform.userphone": phoneData.phoneNumber
                    })
                }
                // else if (data.code==10000){
                   
                // }
                else{
                    wx.showToast({
                        title: '获取号码失败',
                        icon: 'none',
                        duration: 2000
                    })
                }
            });
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function (e) {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        console.log("tempUrl", app.globalData.tempUrl)
        if (this.data.isOpen) {
            return;
        }
        if (app.globalData.tempUrl) {
            var userInform = this.data.userInform;
            userInform.userimg = app.globalData.tempUrl;
            this.setData({
                userInform: userInform
            });
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        this.data.isOpen = false;
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        var userInfo = app.globalData.userInfo;
        //判断当前用户是否开通服务,开通则分享美名片页,否则分享访问记录页
        if (userInfo == null || typeof (userInfo) == "undefined") {
            app.getUserall(function (res) {
                userInfo = res;
            });
        }
        var path = userInfo.UserType == 2 ? "/pages/index/index?cid=" + userInfo.Id : "/pages/cardrecord/cardrecord";
        return {
            title: userInfo.NickName + "的美名片",
            path: path,
            success: function (res) {
                wx.showToast({
                    title: '分享成功',
                    icon: "success",
                    duration: 200
                })
            }
        }
    }
})